//
//  LoginViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisteredViewController.h"
#import "PasswordViewController.h"
#import <CommonCrypto/CommonDigest.h>
#import "MyHandle.h"

@interface LoginViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) LoginView *login;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.login = [[LoginView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    
    
    UIButton *butt = (UIButton *)[_login viewWithTag:500];
    [butt addTarget:self action:@selector(login:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *passWard = (UIButton *)[_login viewWithTag:1000];
    [passWard addTarget:self action:@selector(Knowpassword:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *Reg = (UIButton *)[_login viewWithTag:1001];
    [Reg addTarget:self action:@selector(regist:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:_login];
    
}

- (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ]; 
}

//点击登录触发的方法
- (void)login:(UIButton *)butt{
    //通过tag值 获取到用户输入的邮箱和密码
    UITextField *text1 = (UITextField *)[_login viewWithTag:100];
    UITextField *text2 = (UITextField *)[_login viewWithTag:101];
    
    
    if([self validateEmail:text1.text]){
        if([self validatePassword:text2.text]){
            NSString *email=[text1.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
             AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
            //申明请求的数据是json类型
            manager.requestSerializer=[AFJSONRequestSerializer serializer];
            //传入的参数
            NSDictionary *parameters = @{@"app_version":@"2.3.7",@"device_type":@"1",@"email":email,@"password":[self md5:text2.text]};
            //你的接口地址
            NSString *url=@"http://tubu.ibuzhai.com/rest/v1/sso/login";
            //发送请求
            [LORequestManger POST:url params:parameters success:^(id response) {
                NSDictionary *dic = (NSDictionary *)response;
                
                NSLog(@"登录成功");
                [MyHandle shareHandle].userInfo = dic;
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"用户名密码错误" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
                [aa show];
                [aa dismissWithClickedButtonIndex:3 animated:YES];
                return;
            }];
            
            
        
            
        }else{
            UIAlertView *aa = [[UIAlertView alloc] initWithTitle:@"密码6-10位" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [aa show];
            [aa dismissWithClickedButtonIndex:3 animated:YES];
            return;
        }
    }else{
        UIAlertView *bb = [[UIAlertView alloc] initWithTitle:@"邮箱格式不正确" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [bb show];
        [bb dismissWithClickedButtonIndex:3 animated:NO];
        return;
    }
    
    //判断邮箱格式和密码是否符合要求
//    if([self validateEmail:text1.text]&&[self validatePassword:text2.text]){
//        
//    }
}

//点击忘记密码的方法
- (void)Knowpassword:(UIButton *)pass{
    PasswordViewController *cont = [[PasswordViewController alloc] init];
    [self.navigationController pushViewController:cont animated:NO];
}

//点击立即注册的方法
- (void)regist:(UIButton *)regist{
    RegisteredViewController *re = [[RegisteredViewController alloc] init];
    [self.navigationController pushViewController:re animated:NO];
}

//邮箱正则表达式
- (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
//密码正则
- (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
